﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using TrenitaliaWP7.Resources;
using Services.Menu;
using Utilities;
using Microsoft.Phone.Tasks;

namespace TrenitaliaWP7
{
    class CreditsMenu : OrientableScreen
    {
        
        ArrayList<MyButton> elements;

        MyButton menu;       
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        bool transition_complete;
        Texture2D logo;
        //var emptyTexture = Game.Content.Load<Texture2D>("emptyPixel");
        SpriteFont font;
        SpriteFont smallfont;
        //SpriteFont underlined_font;
        SpriteFont titleFont;
        //var box = Game.Content.Load<Texture2D>("bar_box");
        Texture2D circle;
        Texture2D back;
        String version;
        MyButton suggestionButton;
        public CreditsMenu(Game game)
            : base(game)
        {
        }
        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            elements = new ArrayList<MyButton>();
            version = Strings.Version + (StateManager.isTrial ? "Trial" : "2.8");

            base.Initialize();
        }
        protected override void LoadContent()
        {
            logo = Game.Content.Load<Texture2D>("Menu/vst_logo");
            //var emptyTexture = Game.Content.Load<Texture2D>("emptyPixel");
            font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            smallfont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Small");
            //underlined_font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_underlined");
            titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            //var box = Game.Content.Load<Texture2D>("bar_box");
            circle = Game.Content.Load<Texture2D>("circle");
            back = Game.Content.Load<Texture2D>("back");
            base.LoadContent();
        }

        int transition_velocity = 100;
       
        public override void Update(GameTime gameTime)
        {
            if (elements[0].rectangle.Y <= 10)
                transition_complete = true;
            if (!transition_complete)
            {
                
                var deltay = -(int)(transition_velocity * (float)gameTime.ElapsedGameTime.TotalSeconds)*2;
                
                menu.rectangle.Y += deltay;
                for (int i = 0; i < elements.Count; i++)
                    elements[i].rectangle.Y += deltay;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                //StateManager.SelectMainMenu();
                StateManager.SelectMainMenu();
                return;
            } 
            base.Update(gameTime);
        }
        Color barColor = new Color(31, 31, 31);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            for (int i = 0; i < elements.Count; i++)
                elements[i].Draw(spriteBatch);
            menu.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        //Point tp = new Point();

        int[] elements_y;
        int menu_y;

        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;

            if (StateManager.isTrial)
            {
                if (RectContaintsVect(Game1.trial_rectangle, tap_position))
                {
                    OpenMarket(Game1.APP_ID);
                    return;
                }
            }
            else if (suggestionButton != null && suggestionButton.tapped_on(tap_position))
            {
                //OpenKeyboard();

                EmailComposeTask ect = new EmailComposeTask();

                ect.Subject = "Informa Treni - " + Strings.Suggestion + " - " + DateTime.Now.ToString();
                ect.To = "boscariol.francesco@gmail.com";

                ect.Body = "Informa Treni - " + Strings.Suggestion + " - " + DateTime.Now.ToString() + "\n\n";

                ect.Show();

            }
            if (!transition_complete)
            {
                menu.rectangle.Y = menu_y;
                for (int i = 0; i < elements.Count; i++)
                    elements[i].rectangle.Y = elements_y[i];
                transition_complete = true;
            }
            else if (menu.tapped_on(tap_position))
                //StateManager.SelectMainMenu();
                StateManager.SelectMainMenu();
        }

        //int[] elements_y_p = { 0, 100, 300, 350, 400, 450, 500 };
        //int[] elements_y_l = { 0, 60, 180, 220, 260, 300, 340 };
        int[] elements_y_p = { 0, 100, 200, 240, 280, 320, 530, 560, 610, 660, 360, 400, 440, 480 };
        int menu_y_p = 720;

        public override void SetPortraitOrientation()
        {
            
            

            elements.Clear();
            elements = new ArrayList<MyButton>();
            elements.Add(new MyButton(new Rectangle(0, 800 + elements_y_p[0], 480, 100), Strings.AppTitle, Color.White, titleFont));
            elements.Add(new MyButton(new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2,
                800 + elements_y_p[1], logo.Width, logo.Height),
                logo, Color.White));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[2], 280, 80), Strings.Developers, Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[3], 280, 80), "Francesco Boscariol", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[4], 280, 80), "Giuseppe Maggiore", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[5], 280, 80), "Andrea Gasparetto", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[6], 280, 80), Strings.NoAffiliatedWith1, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[7], 280, 80), Strings.NoAffiliatedWith2, Color.White, smallfont, TextPos.Center));

            //elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[8], 280, 80), Strings.Email, Color.White, smallfont, TextPos.Center));

            suggestionButton = new MyButton(new Rectangle(100, 800 + elements_y_p[8], 280, 80), Strings.Email, Color.White, smallfont, TextPos.Center);
            elements.Add(suggestionButton);

            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[9], 280, 80), version, Color.White, font, TextPos.Center));

            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[10], 280, 80), Strings.GPS_Policy1, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[11], 280, 80), Strings.GPS_Policy2, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[12], 280, 80), Strings.GPS_Policy3, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y_p[13], 280, 80), Strings.GPS_Policy4, Color.White, smallfont, TextPos.Center));

            menu = new MyButton(new Rectangle(215, 800 + menu_y_p, 50, 50), back, Color.White, circle, Color.White);
            transition_complete = false;
            elements_y = elements_y_p;
            menu_y = menu_y_p;
        }

        int[] elements_y_l = { 0, 60, 170, 200, 340, 370, 420, 235, 260, 285, 310};

        int menu_y_l = 420;
        public override void SetLandScapeOrientation()
        {
            elements.Clear();
            elements = new ArrayList<MyButton>();
            elements.Add(new MyButton(new Rectangle(0, 480 + elements_y_l[0], 800, 40), Strings.AppTitle, Color.White, titleFont));
            elements.Add(new MyButton(new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2,
                480 + elements_y_l[1], logo.Width, logo.Height),
                logo, Color.White));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[2], 400, 60), Strings.Developers, Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[3], 400, 60), "Francesco Boscariol, Giuseppe Maggiore, Andrea Gasparetto", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[4], 400, 60), Strings.NoAffiliatedWith1 + " " + Strings.NoAffiliatedWith2, Color.White, smallfont, TextPos.Center));
            
            //elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[5], 400, 60), Strings.Email, Color.White, smallfont, TextPos.Center));
            suggestionButton = new MyButton(new Rectangle(200, 480 + elements_y_l[5], 400, 60), Strings.Email, Color.White, smallfont, TextPos.Center);
            elements.Add(suggestionButton);

            elements.Add(new MyButton(new Rectangle(400, 480 + elements_y_l[6], 400, 50), version, Color.White, font, TextPos.Center));

            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[7], 400, 60), Strings.GPS_Policy1, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[8], 400, 60), Strings.GPS_Policy2, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[9], 400, 60), Strings.GPS_Policy3, Color.White, smallfont, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(200, 480 + elements_y_l[10], 400, 60), Strings.GPS_Policy4, Color.White, smallfont, TextPos.Center));

            menu = new MyButton(new Rectangle(375, 480 + menu_y_l, 50, 50), back, Color.White, circle, Color.White);
            transition_complete = false;
            elements_y = elements_y_l;
            menu_y = menu_y_l;
        }
    }
}
